//
//  WCImageView +setImage.m
//  WC
//
//  Created by wsj2015 on 15/9/7.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCImageView +setImage.h"

#import "JTSImageInfo.h"
#import "JTSImageViewController.h"
@implementation UIImageView (setImage)

- (NSString *)getBaseUrl{
    
    NSString *path =[[NSBundle mainBundle] pathForResource:@"urlConfig" ofType:@"plist"];
    NSDictionary *plistDict =[NSDictionary dictionaryWithContentsOfFile:path];
//    NSString *priod = plistDict[@"MODEL"];
   
    NSString *priod = nil;
#if DEBUG
    priod = plistDict[@"MODEL"];
    
    
#else
    priod =@"ali";
    
#endif
    return [plistDict[@"MEDIAS"] objectForKey:priod];;
}
+ (NSString *)getBaseUrl{
    
    NSString *path =[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
    NSDictionary *plistDict =[NSDictionary dictionaryWithContentsOfFile:path];
    //    NSString *priod = plistDict[@"MODEL"];
    
    NSString *priod = nil;
#if DEBUG
    priod = plistDict[@"MODEL"];
    
    
#else
    priod =@"ali";
    
#endif
    return [plistDict[@"MEDIAS"] objectForKey:priod];;
}


-(void)setImageWithUrl:(NSString *)url
{
   
    NSString *mainUrl =[NSString stringWithFormat:@"%@/image/img/%@",[self getBaseUrl],url];
    [self sd_setImageWithURL:[NSURL URLWithString:mainUrl] placeholderImage:[UIImage imageNamed:@"暂无图片"]];
   
}
-(void)setCateImageWithUrl:(NSString *)url
{
//    image/img/ 
    NSString *path =[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
    NSDictionary *plistDict =[NSDictionary dictionaryWithContentsOfFile:path];
    NSString *priod = plistDict[@"MODEL"];
    NSString *bathUrl =[plistDict[@"MEDIAS"] objectForKey:priod];
    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/simg/",url];
    [self sd_setImageWithURL:[NSURL URLWithString:newStr] placeholderImage:[UIImage imageNamed:@"科室_03"]];


}
-(void)setOrderInforImageWithUrl:(NSString *)url
{
    
    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/simg/",url];
    [self sd_setImageWithURL:[NSURL URLWithString:newStr]];
    
        
}
- (void)downLoadOriImage:(NSString *)url completed:(void(^)(UIImage *image))comleteBolck
{
    
  
    
    
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:url]  options:SDWebImageProgressiveDownload | SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (comleteBolck) {
            if (image) {
                comleteBolck(image);
            }else{
                //                 comleteBolck([UIImage imageNamed:@"nurse"]);
            }
        }
    }];
   
  
    
}
- (void)zoomImageFromUrl:(NSString *)imageAvtarId fromViewController:(UIViewController *)vc{
    cwqHud(self);
    [self downLoadOriImage:imageAvtarId completed:^(UIImage *image) {
        [hud setHidden:YES];
        JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
        imageInfo.image = image;
        imageInfo.referenceRect = self.frame;
        imageInfo.referenceView = self.superview;
        imageInfo.referenceContentMode = self.contentMode;
        imageInfo.referenceCornerRadius = self.layer.cornerRadius;
        
        // Setup view controller
        JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
                                               initWithImageInfo:imageInfo
                                               mode:JTSImageViewControllerMode_Image
                                               backgroundStyle:JTSImageViewControllerBackgroundOption_Scaled];
        
        
        [imageViewer showFromViewController:vc transition:JTSImageViewControllerTransition_FromOriginalPosition];
        
        
    }];
    
    
}

- (void)zoomImagefromImage:(UIImage *)image fromViewController:(UIViewController *)vc{
    
    
    
    JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
    imageInfo.image = image;
    imageInfo.referenceRect = self.frame;
    imageInfo.referenceView = self.superview;
    imageInfo.referenceContentMode = self.contentMode;
    imageInfo.referenceCornerRadius = self.layer.cornerRadius;
    
    // Setup view controller
    JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
                                           initWithImageInfo:imageInfo
                                           mode:JTSImageViewControllerMode_Image
                                           backgroundStyle:JTSImageViewControllerBackgroundOption_Scaled];
    
    
    [imageViewer showFromViewController:vc transition:JTSImageViewControllerTransition_FromOriginalPosition];
    
    
    
    
    
}

-(void)wsjSetImageWithUrl:(NSString *)url withPlaceImageName:(NSString *)placeImageName
{

    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/simg/",url];
    [self sd_setImageWithURL:[NSURL URLWithString:newStr] placeholderImage:[UIImage imageNamed:placeImageName]];
    
}
-(void)wsjSetBigImageWithUrl:(NSString *)url withPlaceImageName:(NSString *)placeImageName successBlock:(void(^)(CGSize imageSize))successBlock
{
    
    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/img/",url];
    //    [self sd_setImageWithURL:[NSURL URLWithString:newStr] placeholderImage:[UIImage imageNamed:placeImageName]];
    UIImage *cacheimage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:newStr];
    if (cacheimage) {
        self.image =cacheimage;
        if (successBlock) {
            successBlock(cacheimage.size);
        }
        return;
    }
    [self sd_setImageWithURL:[NSURL URLWithString:newStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (successBlock) {
            successBlock(image.size);
        }
        
    }];
    
}
-(void)wsjSetBigImageWithUrl:(NSString *)url withPlaceImageName:(NSString *)placeImageName successImageBlock:(void(^)(UIImage *image))successImageBlock{
  
    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/img/",url];
    //    [self sd_setImageWithURL:[NSURL URLWithString:newStr] placeholderImage:[UIImage imageNamed:placeImageName]];
    UIImage *cacheimage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:newStr];
    if (cacheimage) {
        self.image =cacheimage;
        if (successImageBlock) {
            successImageBlock(cacheimage);
        }
        return;
    }
    [self sd_setImageWithURL:[NSURL URLWithString:newStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (successImageBlock) {
            successImageBlock(image);
        }
        
    }];

}
-(void)wsjSetBigImageWithUrl:(NSString *)url successBlock:(void(^)(UIImage  *image))successBlock
{
    
    NSString *newStr =[NSString stringWithFormat:@"%@%@%@",[self getBaseUrl],@"/image/img/",url];
    //    [self sd_setImageWithURL:[NSURL URLWithString:newStr] placeholderImage:[UIImage imageNamed:placeImageName]];
    UIImage *cacheimage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:newStr];
    if (cacheimage) {
        self.image =cacheimage;
        if (successBlock) {
            successBlock(cacheimage);
        }
        return;
    }
    [self sd_setImageWithURL:[NSURL URLWithString:newStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (successBlock) {
            successBlock(image);
        }
        
    }];
    
}
+ (NSString *)getimageUrlForMedia
{

    NSString *newStr =[NSString stringWithFormat:@"%@%@",[UIImageView getBaseUrl],@"/image/simg/"];
    return newStr;
    
}

@end
